package com.sy.core.interceptor;

import com.sy.core.exception.AuthorizationException;
import com.sy.core.model.SysUser;
import com.sy.core.web.BaseCtl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor extends BaseCtl implements HandlerInterceptor {
    @Resource
    private RedisTemplate<Object, Object> template;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String url = request.getRequestURI();
        String token = request.getHeader("Authorization");
        if(StringUtils.isNotBlank(token)){
            SysUser sysUser = (SysUser) template.opsForValue().get(token);
            if(sysUser!=null){
                return true;
            }else{
                throw new AuthorizationException("登录信息已过期,请重新登录");
            }
        }else{
            throw new AuthorizationException("登录信息已过期,请重新登录");
        }
       //return true;
    }
}
